package echat.future.cn.echat.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.modelmsg.ShowMessageFromWX;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import echat.future.cn.echat.AppApplication;
import echat.future.cn.echat.my.utils.Contancts;
import echat.future.cn.echat.my.utils.ToastUtils;

public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
    IWXAPI iwxapi;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        iwxapi = WXAPIFactory.createWXAPI(this, Contancts.WECHAT_APPID,true);
        iwxapi.handleIntent(getIntent(),this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        iwxapi.handleIntent(intent,this);
    }

    @Override
    public void onReq(BaseReq baseReq) {
        Log.i("Load",baseReq.openId);
    }

    // 第三方应用发送到微信的请求处理后的响应结果，会回调到该方法
    @Override
    public void onResp(BaseResp resp) {
        switch (resp.errCode){
            case 0:
                if(resp instanceof SendAuth.Resp){
                    AppApplication.wxLoginBean.setCode(((SendAuth.Resp)(resp)).code);
                    finish();
                }
                break;
            case -4:
                ToastUtils.toastMsg(this,"用户拒绝授权");
                break;
            case -2:
                ToastUtils.toastMsg(this,"用户取消授权");
                break;
        }
        finish();
    }

}
